package com.huobao.yunying.huanxin.ui.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.easemob.easeui.ui.EaseChatFragment;
import com.huobao.yunying.base.HuobaoApplication;
import com.huobao.yunying.entity.DetailGoodsData;
import com.huobao.yunying.entity.DingdanDetail;
import com.huobao.yunying.huanxin.ui.Constant;
import com.huobao.yunying.utils.PermissionsChecker;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;

/**
 * 聊天页面，需要fragment的使用{@link EaseChatFragment}
 */
public class ChatActivity extends BaseActivity {


	public static ChatActivity activityInstance;
	private ChatFragment chatFragment;
	private final String toChatUsername = "liyanlong";

	private boolean check1,check2;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.em_activity_chat);
		activityInstance = this;
		chatFragment = new ChatFragment();
		Intent intent = getIntent();
		DetailGoodsData goodsData = (DetailGoodsData) getIntent().getSerializableExtra("data");
		intent.putExtra(Constant.EXTRA_USER_ID, toChatUsername);
		intent.putExtra(Constant.EXTRA_SHOW_USERNICK, true);
		if (goodsData != null)
			intent.putExtra("data", goodsData);
		DingdanDetail detail = (DingdanDetail) getIntent().getSerializableExtra("dingdan");
		if (detail != null)
			intent.putExtra("dingdan", detail);
		// 传入参数
		chatFragment.setArguments(intent.getExtras());
		getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		activityInstance = null;
	}

	public void requestPermission(){
		try{
			int pid = android.os.Process.myPid();
			String processAppName = ((HuobaoApplication)getApplication()).getAppName(pid);
			if (processAppName != null && processAppName.equalsIgnoreCase("com.huobao.zhangying")) {
				if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
					if(requestPermission1()){
						if(requestPermission2()){
						}
					}
				}
			}
		}catch(Exception e){

		}
	}
	@Override
	protected void onResume() {
		super.onResume();
//		requestPermission();
	}
	public boolean requestPermission1(){
		if (!PermissionsChecker.lacksPermission(this,PermissionsChecker.PERMISSION_1)){
			return true;
		}
		BaseDialog.showPermissionDialog(this, getString(R.string.permission_kefu1), PermissionsChecker.PERMISSION_1,1, null);
		return false;
	}
	private boolean requestPermission2(){
		if (!PermissionsChecker.lacksPermission(this,PermissionsChecker.PERMISSION_2)){
			return true;
		}
		BaseDialog.showPermissionDialog(this, getString(R.string.permission_kefu2), PermissionsChecker.PERMISSION_2,2, null);
		return false;
	}
	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		if(requestCode == 1 && requestPermission1()){
			requestPermission2();
		}
	}
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		// 点击notification bar进入聊天页面，保证只有一个聊天页面
		String username = intent.getStringExtra("userId");
		if (toChatUsername.equals(username))
			super.onNewIntent(intent);
		else {
			finish();
			startActivity(intent);
		}

	}

	@Override
	public void onBackPressed() {
		chatFragment.onBackPressed();
	}

	public void sendRobotMessage(String txtContent, String menuId) {
		chatFragment.sendRobotMessage(txtContent, menuId);
	}

}
